#include <header.h>

pthread_mutex_t lock1;
pthread_mutex_t lock2;

void *func(void  *p){

    while(1){
        pthread_mutex_lock(&lock2);
        sleep(1);
        int res = pthread_mutex_trylock(&lock1);
        if(res != 0){
            // 说明这一次trylock, 没有拿到锁1
            // 为了避免死锁, 可以释放锁2
            pthread_mutex_unlock(&lock2);
            continue;
        }

        printf("i am son_thread \n");

        pthread_mutex_unlock(&lock1);
        pthread_mutex_unlock(&lock2);
        break;
    }
    return NULL;
}
int main(int argc,char*argv[])
{

    pthread_mutex_init(&lock1, NULL);
    pthread_mutex_init(&lock2, NULL);

    pthread_t son_id;
    pthread_create(&son_id, NULL, func, NULL);

    pthread_mutex_lock(&lock1);
    sleep(1);
    pthread_mutex_lock(&lock2);

    printf("i am main \n");

    pthread_mutex_unlock(&lock2);
    pthread_mutex_unlock(&lock1);

    pthread_join(son_id, NULL);
    return 0;
}